package com.quasar.hpatient.view.prescription;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.medicalmodel.PrescriptionDayData;
import com.quasar.hpatient.onInterface.OnSelectPrescriptionListener;
import com.quasar.hpatient.presenter.SelectPrescriptionPresenter;
import com.quasar.hpatient.view.adapter.HistoryPrescriptionAdapter;
import com.quasar.hpatient.widget.dialog_my_date_code;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 历史药单
 */
@EActivity(R.layout.activity_history_prescription)
public class HistoryPrescriptionActivity extends BaseActivity implements  OnSelectPrescriptionListener,dialog_my_date_code.OnCustomDialogDetermineListener,BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;

    private int delayMillis = 1000;//设置刷新等待的时间
    //设置分页
    private int page=1;
    //判断下一页是否存在数据
    private boolean onLoadMore=false;
    SelectPrescriptionPresenter selectPrescriptionPresenter;
    HistoryPrescriptionAdapter historyPrescriptionAdapter;


    @AfterViews
    void init() {
        initView();
        initData();
    }


    //初始化adapter布局
    public void initAdapter() {
        historyPrescriptionAdapter = new HistoryPrescriptionAdapter();
        historyPrescriptionAdapter.setOnLoadMoreListener(this, rv_list);
        rv_list.setAdapter(historyPrescriptionAdapter);
        selectPrescriptionPresenter.GetPatientSinglePrescrptionDetailData(page+"");
        //item点击
        historyPrescriptionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    @Override
    protected void initView() {
        initToolbar();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        selectPrescriptionPresenter = new SelectPrescriptionPresenter(this);
        initAdapter();

    }

    //查询历史成功
    @Override
    public void onSeccessHistory(PrescriptionDayData data) {
        //判断page是否是下拉还是上拉

        if(data.getList()!=null && data.getList().size()>0){
            if(data.getList().get(0)!=null){
                if(data.getList().get(0).getPerscriptDetailList()!=null){
                    if(page==1){
                        historyPrescriptionAdapter.setNewData(data.getList().get(0).getPerscriptDetailList());
                        onLoadMore=false;
                    }else{
                        historyPrescriptionAdapter.addData(data.getList().get(0).getPerscriptDetailList());
                        if(data.getList().size()==0){
                            onLoadMore=true;
                        }
                    }
                }
            }else{
                onLoadMore=true;
            }
        }else{
            onLoadMore=true;
        }
    }

    @Override
    public void onPrescriptionDaySuccess(PrescriptionDayData data) {

    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        historyPrescriptionAdapter.notifyDataSetChanged();
    }


    @Override
    public void DialogDetermine(String statsTime, String endTime) {
        ViseLog.d("开始时间"+statsTime+"结束时间"+endTime);
//        selectPrescriptionPresenter.GetPatientSinglePrescrptionDetailData(statsTime, endTime);
    }

    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        page=1;
        selectPrescriptionPresenter.GetPatientSinglePrescrptionDetailData(page+"");
        historyPrescriptionAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                historyPrescriptionAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;//下拉时page递增
        selectPrescriptionPresenter.GetPatientSinglePrescrptionDetailData(page+"");
        rv_list.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当返回的数据为空时停止下拉刷新
                if (onLoadMore) {
                    historyPrescriptionAdapter.loadMoreEnd();
                } else {
                    historyPrescriptionAdapter.loadMoreComplete();
                    historyPrescriptionAdapter.notifyDataSetChanged();
                }
            }

        }, delayMillis);
    }
}
